package ifpb.pp.rh.report;



/**
 *
 * @author Jarddel Antunes
 */

import ifpb.pp.rh.entity.Servidor;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletContext;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.*;
import net.sf.jasperreports.view.JasperViewer;


public class RelatorioServidor {
    private Integer codServidor;
    private String nomeServidor;
    private String matServidor;
    private static List <Servidor> listServidor;



    public static void execute(ServletContext sc, HttpServletRequest request, HttpServletResponse response) {
        try {
            List <RelatorioServidor> listRelServidor = new ArrayList<RelatorioServidor>();

//            listServidor = PEGAR LISTA DE SERVIDOR

            for(int i=0; i < listServidor.size(); i++){
                RelatorioServidor relServidor = new RelatorioServidor();
                relServidor.setCodServidor(listServidor.get(i).getId());
                relServidor.setNomeServidor(listServidor.get(i).getNome());
                listRelServidor.add(relServidor);
            }

            JRDataSource jrds = new JRBeanCollectionDataSource(listRelServidor);

            // parametros do relatorio
            HashMap parameters = new HashMap();
            // lendo arquivo jasper
            File reportFile = new File(sc.getRealPath("/rel/servidores.jasper"));
            //String fileName ="teste";
            byte[] bytes = null;
            try {
                bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, jrds);
                //JasperPrint print = JasperFillManager.fillReport(reportFile.getPath(), parameters, jrds);

                 //bytes = JasperExportManager.exportReportToPdf(print);
                //JasperViewer.viewReport( reportFile.getName() + ".pdf", false );
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            //
            if (bytes != null && bytes.length > 0) {
                response.setContentType("application/pdf");
                response.setContentLength(bytes.length);
                ServletOutputStream ouputStream = response.getOutputStream();
                ouputStream.write(bytes, 0, bytes.length);
                ouputStream.flush();
                ouputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Servidor> getListServidor() {
        return listServidor;
    }

    public void setListDepto(List<Servidor> listServidor) {
        this.listServidor = listServidor;
    }

    public Integer getIdServidor() {
        return codServidor;
    }

    public void setCodServidor(Integer codServidor) {
        this.codServidor = codServidor;
    }

    public String getNomeServidor() {
        return nomeServidor;
    }

    public void setNomeServidor(String nomeServidor) {
        this.nomeServidor = nomeServidor;
    }

}